const query=require("../db/mysql")


class cartList{
    getCart=async (ctx)=>{
        const {id}=ctx.query
        console.log(id)
        let sql=`select * from cart where 1=1 `

        if(id){
            sql+=`and id="${id}" `
        }
        const data=await query(sql)

        ctx.body={
            data:data
        }
    }


    addCart=async (ctx)=>{
        const {id}=ctx.request.body
        console.log(id)
        let ishave=`select * from shop where id="${id}" `
        const data=await query(ishave)
        console.log(data)
        let iscart=`select * from cart where id="${id}" `
        const data2=await query(iscart)
        console.log(data2,"2")
        let sql=""
        if(data2.length){
             sql=`update cart set num="${data2[0].num+=1}" where id="${id}" `
        }else{
             sql=`insert into cart(shopname,shoptitle,shopprice,shopimg,id,num) values("${data[0].shopname}","${data[0].shoptitle}","${data[0].shopprice}","${data[0].shopimg}","${data[0].id}","${data[0].num}") `

        }

        const adddata=await query(sql)
        ctx.body={
            data:adddata
        }
        
    }


    delCart=async (ctx)=>{
        const {id}=ctx.params

        console.log(id)
        let sql=`delete from cart where id="${id}" `
        const data=await query(sql)
        ctx.body={
            data:data
        }
    }
}

module.exports=new cartList()